//
//  BZYDropDownView.m
//  BZYMeituan
//
//  Created by 毕仲阳 on 16/9/15.
//  Copyright © 2016年 mycompany. All rights reserved.
//

#import "BZYDropDownView.h"
#import "BZYLeftDropDownCell.h"
#import "BZYRightDropDownCell.h"
#import "BZYCategoryModel.h"
#import "BZYDistrictModel.h"
@interface BZYDropDownView ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *leftTableView;
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;

//分类左边选中的模型
@property (nonatomic,strong) BZYCategoryModel *leftSelectedCategoryModel;
//区域左边选中的模型
@property (nonatomic,strong) BZYDistrictModel *leftSelectedDistrictModel;
@end
@implementation BZYDropDownView

+ (instancetype)dropDownView
{
    UINib *nib = [UINib nibWithNibName:@"BZYDropDownView" bundle:nil];
    return  [nib instantiateWithOwner:nil options:nil].firstObject;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.autoresizingMask = UIViewAutoresizingNone;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.categoryArray) {//分类数据有值
        if (tableView == self.leftTableView) {
            return self.categoryArray.count;
        }else
        {
            return self.leftSelectedCategoryModel.subcategories.count;
        }
    }else //区域数据有值
    {
        if (tableView == self.leftTableView) {
            return  self.districtArray.count;
        } else {
            return self.leftSelectedDistrictModel.subdistricts.count;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.categoryArray) {
        if (tableView == self.leftTableView) {
            BZYLeftDropDownCell *cell = [BZYLeftDropDownCell leftDropDownCellWithTableView:tableView];
            BZYCategoryModel *model = self.categoryArray[indexPath.row];
            cell.textLabel.text = model.name;
            cell.imageView.image = [UIImage imageNamed:model.icon];
            cell.imageView.highlightedImage = [UIImage imageNamed:model.highlighted_icon];
            
            if (model.subcategories.count == 0) {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }else
            {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            
            return cell;
        }else
        {
            BZYRightDropDownCell *cell = [BZYRightDropDownCell rightDropDownCellWithTableView:tableView];
            NSArray *categoryArray = self.leftSelectedCategoryModel.subcategories;
            cell.textLabel.text = categoryArray[indexPath.row];
            return cell;
        }

    }else
    {
        if (tableView == self.leftTableView) {
            BZYLeftDropDownCell *cell = [BZYLeftDropDownCell leftDropDownCellWithTableView:tableView];
            BZYDistrictModel *model = self.districtArray[indexPath.row];
            cell.textLabel.text = model.name;
           
            if (model.subdistricts.count == 0) {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }else
            {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            
            return cell;
        }else
        {
            BZYRightDropDownCell *cell = [BZYRightDropDownCell rightDropDownCellWithTableView:tableView];
            NSArray *categoryArray = self.leftSelectedDistrictModel.subdistricts;
            cell.textLabel.text = categoryArray[indexPath.row];
            return cell;
        }

    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.categoryArray) {
        if (tableView == self.leftTableView) {
            
            self.leftSelectedCategoryModel = self.categoryArray[indexPath.row];
            
            if (self.leftSelectedCategoryModel.subcategories.count == 0) {
                [[NSNotificationCenter defaultCenter] postNotificationName:BZYCategoryDidChangeNotification object:nil userInfo:@{BZYCategoryModelKey : self.leftSelectedCategoryModel}];
            }
        }
        else {//右边分类
            [[NSNotificationCenter defaultCenter] postNotificationName:BZYCategoryDidChangeNotification object:nil userInfo:@{BZYCategoryModelKey : self.leftSelectedCategoryModel,BZYCategorySubtitleKey : self.leftSelectedCategoryModel.subcategories[indexPath.row] }];
        }
        
    }else
    {
        if (tableView == self.leftTableView) {//选择区域左边
            
            self.leftSelectedDistrictModel = self.districtArray[indexPath.row];
            
            //刷新右侧 tableView 数据源
            //[self.rightTableView reloadData];
            if (self.leftSelectedDistrictModel.subdistricts.count == 0) {
                [[NSNotificationCenter defaultCenter] postNotificationName:BZYDistrictDidChangeNotification object:nil userInfo:@{BZYDistrictModelKey : self.leftSelectedDistrictModel}];
            }
            
        }else
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:BZYDistrictDidChangeNotification object:nil userInfo:@{BZYDistrictModelKey : self.leftSelectedDistrictModel,BZYDistrictSubtitleKey : self.leftSelectedDistrictModel.subdistricts[indexPath.row]}];
        }
    }
     [self.rightTableView reloadData];
}
@end
